package game 
{
	import flash.display.*;
	import flash.events.*;
	import flash.text.*;
	import flash.utils.*;
	import game.birds.*;
	import windowsfla.*;
	/**
	 * 
	 * Moorhuhn 
	 * Перезарядка - Пробел
	 * Стрелять - левая кнопка мыши
	 * Птицы врезаются в друг друга
	 * 
	 * @author Goretskiy Alex
	 */
	public class Game implements IWindow
	{
		//--------------------------------------------------------------------------
		//
		//  Variables
		//
		//--------------------------------------------------------------------------
		
		//----------------------------------
		//  Static variables
		//----------------------------------
		
		private static var _instance:Game;
		
		//----------------------------------
		//  Private variables
		//----------------------------------
		
		private var _timer:Timer;
		
		//----------------------------------
		//  Protected variables
		//----------------------------------
		
		protected var _window:MovieClip;
		
		/** Прицел */
		protected var _sight:SightMC;
		
		/** Массив птиц на экране */
		protected var _birds:Dictionary;
		
		/** Кол-во патронов */
		protected var _ammunition:int;
		
		/**  Прорисовка патронов */
		protected var _ammunition_complect:MovieClip;
		
		/** Очки */
		protected var _scores:int;
		
		//--------------------------------------------------------------------------
		//
		//  Getters and setters
		//
		//--------------------------------------------------------------------------
		
		public function get window():MovieClip { return _window; }
		public function get sight():MovieClip 
		{
			const sight_kill:MovieClip = Utils.getChildByNameDeep( _sight, "sight_kill" ) as MovieClip;
			return sight_kill; 
		}
		
		public static function get instance():Game 
		{
			if ( !_instance )
			{
				_instance = new Game();
			}
			
			return _instance;
		}
		
		public function get ammunition():int { return _ammunition; }
		public function set ammunition( value:int ):void 
		{
			_ammunition = value;
			redrawAmmunitionComplect();
		}
		
		public function get birds():Dictionary { return _birds; }
		
		public function get scores():int { return _scores; }
		public function set scores(value:int):void 
		{
			_scores = value;
			const scores_text:TextField = Utils.getChildByNameDeep( window, "scores_text" ) as TextField;
			scores_text.text = String( _scores );
		}
		
		//--------------------------------------------------------------------------
		//
		//  Methods
		//
		//--------------------------------------------------------------------------
		
		//----------------------------------
		//  Constructor
		//----------------------------------
		
		public function Game() 
		{
			
		}
		
		//----------------------------------
		//  Public methods
		//----------------------------------
		
		/**
		 * 
		 * @param	stage
		 */
		public function init( stage:Stage ):void
		{
			_window = new GameWindow();
			stage.addChild( _window );
			
			_timer = new Timer( 1000 );
			_timer.addEventListener( TimerEvent.TIMER, startGame );
			_timer.start();
			
			scores = 0;
		}
		
		/**
		 * Начинаем игру по таймеру 
		 * @param	e
		 */
		private function startGame(e:TimerEvent):void 
		{
			_timer.stop();
			
			_birds = new Dictionary();
			initBirds();
			
			_sight = new SightMC();
			const game_window:MovieClip = Utils.getChildByNameDeep( window, "game_window" ) as MovieClip;
			game_window.addChild( _sight );
			_sight.mouseEnabled = false;
			_sight.mouseChildren = false;
			
			_ammunition_complect = new MovieClip();
			game_window.addChild( _ammunition_complect );
			
			ammunition = Settings.MAX_AMMUNITION;
			
			_window.stage.addEventListener( KeyboardEvent.KEY_DOWN, refreshAmmunition );
			_window.stage.addEventListener( MouseEvent.CLICK, shoot );
			_window.stage.addEventListener( Event.ENTER_FRAME, update );
		}
		
		//----------------------------------
		//  Protected methods
		//----------------------------------
		
		/**
		 * Создаем птиц в начале игры
		 */
		protected function initBirds():void 
		{
			const game_window:MovieClip = Utils.getChildByNameDeep( window, "game_window" ) as MovieClip;
			
			var new_bird:*;
			var type:int = 0;
			
			for ( var i:int = 0; i < Settings.BIRDS_ON_SCREEN; i++ )
			{
				type = Utils.randNumber( 0, Settings.BIRDS_TYPES_LIST.length - 1 );
				
				new_bird = Utils.getClassObj( Settings.BIRDS_TYPES_LIST[type] );
				game_window.addChild( new_bird );
				new_bird.init( type );
				
				_birds[new_bird] = new_bird;
			}
		}
		
		/**
		 * Добавляем необходимое кол-во птиц
		 */
		protected function addNewBird():void 
		{
			if ( Utils.countKeys( _birds ) == Settings.BIRDS_ON_SCREEN ) return;
			
			const game_window:MovieClip = Utils.getChildByNameDeep( window, "game_window" ) as MovieClip;
			
			var new_bird:*;
			var type:int = 0;
			
			while ( Utils.countKeys( _birds ) < Settings.BIRDS_ON_SCREEN )
			{
				type = Utils.randNumber( 0, Settings.BIRDS_TYPES );
				
				new_bird = Utils.getClassObj( Settings.BIRDS_TYPES_LIST[Utils.randNumber( 0, Settings.BIRDS_TYPES_LIST.length - 1 )] );
				game_window.addChild( new_bird );
				new_bird.init( type );
				
				_birds[new_bird] = new_bird;
			}
			
			//Заново добавим прицел - так новые птицы будут поверх него
			if ( _sight.parent )
			{
				_sight.parent.removeChild( _sight );
			}
			
			game_window.addChild( _sight );
			_sight.mouseEnabled = false;
			_sight.mouseChildren = false;
		}
		
		//----------------------------------
		//  Events
		//----------------------------------
		
		private function update( e:Event = null ):void 
		{
			const game_window:MovieClip = Utils.getChildByNameDeep( window, "game_window" ) as MovieClip;
			
			_sight.x = game_window.mouseX;
			_sight.y = game_window.mouseY;
			
			for each ( var bird:* in _birds )
			{
				if ( bird == null ) continue;
				
				bird.update();
			}
			
			addNewBird();
		}
		
		/**
		 * Перезарядились
		 * @param	e
		 */
		private function refreshAmmunition( e:KeyboardEvent ):void 
		{
			ammunition = Settings.MAX_AMMUNITION;
			redrawAmmunitionComplect();
		}
		
		/**
		 * Прорисовка патронов
		 */
		private function redrawAmmunitionComplect():void 
		{
			const game_window:MovieClip = Utils.getChildByNameDeep( window, "game_window" ) as MovieClip;
			
			while ( _ammunition_complect.numChildren > 0 )
			{
				_ammunition_complect.removeChildAt( 0 );
			}
			
			var xc:int = 0;
			var amm:AmmunitionUnitMC = new AmmunitionUnitMC();
			
			for (var i:int = 0; i < ammunition; i++) 
			{
				amm = new AmmunitionUnitMC();
				amm.x = xc;
				_ammunition_complect.addChild( amm );
				xc += amm.width * 1.5;
			}
			
			_ammunition_complect.x = 15;
			_ammunition_complect.y = Settings.WORLD_HEIGHT - _ammunition_complect.height;
		}
		
		/**
		 * Сделали выстрел
		 * @param	e
		 */
		private function shoot( e:MouseEvent ):void 
		{
			if ( ammunition > 0 )
			{
				ammunition--;
			}
		}
		
		
	}

}